home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / autologin < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.6 KB  |  141 lines

  1. #!/bin/ksh
  2. # @(#) autologin.ksh 1.1 94/11/16
  3. # 93/11/15 john h. dubois iii (john@armory.com)
  4. # 93/12/31 fixed quoting on final exec line
  5. # 94/02/25 Added -x option
  6. # 94/06/29 Added -hc.  No setpgrp if no controlling tty.  chown/chmod tty.
  7. # 94/10/17 Open all 3 terminal fds for both input & output, for e.g. more
  8. # 94/11/16 Added mnot options.
  9.  
  10. longName=${0##*/}
  11. ttyMode=600
  12. Usage="Usage: 
  13. $longName [-xhno] [-m<tty-mode>] [-t<termtype>] [-c\"command [arg ...]\"]
  14.       <user> <tty> [termio-params]"
  15. unset TERM    # so we can tell if it's set with -t
  16. suArg=-
  17. UserTTY=true
  18.  
  19. while getopts :xhc:t:nm:o opt; do
  20.     case $opt in
  21.     h)
  22.     echo \
  23. "$longName: log in a user on a particular tty.
  24. $Usage
  25. <user> is the user to log in.  <tty> is the tty line to log the user in on.
  26. The default termio parameters set are 'sane' and 'echoe'.
  27. Additional termio parameters can be given after the tty name.
  28. $longName is typically run from inittab so that the user will be logged in on a
  29. tty when the system goes multiuser.  Example inittab line:
  30. Se1f:23:respawn:/bin/autologin johnd tty1f 38400 ixon ixoff
  31. Additional notes:
  32. The user is su'd to, and thus must have a shell that can be exec'd.
  33. If the user's shell is a script, it must be a #! script.
  34. Also, su does not set supplemental groups correctly when invoked by init.
  35. The ut_user field of the utmp entry for the tty will be the name of this
  36. program, rather than that of the user.
  37. Options:
  38. -h: Print this help.
  39. -c<command> <arg> ...: Have user run command after login.  If args are given,
  40.     the command and args should be quoted together.
  41. -n: Start a regular shell for the user, rather than a login shell (do not give
  42.     the '-' argument to su).  If -n is given, the shell will not read its
  43.     startup files (e.g. .profile for sh users).
  44. -o: Do not change the owner of the tty to be the user being logged in on it.
  45.     The tty is opened before the user is su'ed to, so it does not strictly
  46.     need to be owned by the user.  However, some applications reopen the tty,
  47.     so it is usually advisible to use -m to make the tty read/writable by all
  48.     if -o is used.
  49. -m<tty-mode>: The mode, in the style of chmod(C), to chmod the tty to.  The
  50.     default is $ttyMode.  Use '-m \"\"' to not attempt to change the tty mode.
  51. -t<termtype>: Set the value of TERM that will be passed to the user process.
  52.     By default, the TERM value appropriate to the tty is read from
  53.     /etc/ttytype.  TERM must be set correctly to run any application that uses
  54.     a full-screen interface.
  55. -x: Turn on debugging.  If $longName is run from a terminal, debugging output
  56.     is sent to the same terminal.  If not (e.g. when run from inittab), output
  57.     is sent to /dev/console."
  58.     exit 0
  59.     ;;
  60.     c)
  61.     Command="-c $OPTARG";;
  62.     x)
  63.     set -x;;
  64.     m)
  65.     ttyMode=$OPTARG;;
  66.     n)
  67.     suArg=;;
  68.     t)
  69.     TERM=$OPTARG;;
  70.     o)
  71.     UserTTY=false;;
  72.     +?)
  73.     print -u2 "$longName: options should not be preceded by a '+'."
  74.     exit 1
  75.     ;;
  76.     :)
  77.         print -r -u2 -- \
  78.         "$longName: Option '$OPTARG' requires a value.  Use -h for help."
  79.         exit 1
  80.         ;;
  81.     ?) 
  82.     print -u2 "$longName: $OPTARG: bad option.  Use -h for help."
  83.     exit 1
  84.     ;;
  85.     esac
  86. done
  87.  
  88. # remove args that were options
  89. let OPTIND=OPTIND-1
  90. shift $OPTIND
  91.  
  92. if [ $# -lt 2 ]; then
  93.     print -u2 "$Usage\nUse -h for help."
  94.     exit
  95. fi
  96.  
  97. # If not being run from a tty, send errors to console
  98. [ ! -t 1 ] && exec > /dev/console 2>&1
  99.  
  100. USER=$1
  101. tty=$2
  102. shift 2
  103. [[ "$tty" != /* ]] && tty=/dev/$tty
  104. if [ ! -c $tty ]; then
  105.     # Actually only tests if it is a char device.
  106.     print -u2 "$longName: $tty: not a tty."
  107.     exit 4
  108. fi
  109.  
  110. ttyname=${tty##*/}
  111.  
  112. if [ -z "$TERM" ]; then
  113.     # Try to find termtype in database
  114.     ttytype=/etc/ttytype
  115.     [ -f $ttytype ] &&
  116.     egrep "[    ]$ttyname($|[     ])" $ttytype | read TERM garbage
  117. fi
  118. export TERM    # export TERM regardless
  119.  
  120. $UserTTY && chown $USER $tty
  121. [ -n "$ttyMode" ] && chmod $ttyMode $tty
  122.  
  123. # Attach to the tty & set its parameters sanely.
  124. # Then su to the user with '-' to make it act like a login shell.
  125. # All 3 fds must be opened for both read & write because some programs like
  126. # 'more' do funky stuff like opening stdout for input.
  127. FullCmd="exec <>$tty 1>&0 1<&0 2>&0 2<&0;/bin/stty sane echoe $*;
  128. exec /bin/su $suArg $USER $Command"
  129.  
  130. # If we can open /dev/tty we have a controlling tty (have been invoked outside
  131. # of e.g. inittab/sdd) & must use setpgrp to get rid of it so we can get the
  132. # "login" tty as controlling tty.
  133. setpgrp=/bin/setpgrp
  134. if (< /dev/tty) 2>/dev/null && [ -x $setpgrp ]; then
  135.     # Invoke ksh because setpgrp exec's its arg directly & we need shell syntax
  136.     # Use ksh instead of sh because sh doesn't do the fd redirection correctly
  137.     exec $setpgrp /bin/ksh -c "$FullCmd"
  138. else
  139.     eval "$FullCmd"
  140. fi
  141.